package com.jm.bishe.service.Impl;

import com.jm.bishe.entity.*;
import com.jm.bishe.mapper.UserMapper;
import com.jm.bishe.service.UserService;
import com.jm.bishe.utils.JwtTokenUtil;
import com.jm.bishe.utils.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    UserMapper userMapper;


    @Override
    public Result register(UserRegister userRegister) {
        User user;
        try {
            user = new User(System.currentTimeMillis(), MD5Util.convertMD5(MD5Util.desEncrypt(userRegister.getPassWord())),
                    userRegister.getRealName(),new SimpleDateFormat("yyyy-MM-dd").parse(userRegister.getBornDate()),
                    userRegister.getPhoneNumber(),userRegister.getSex(),
                    userRegister.getIDNumber(),0,0);    //存入数据库的密码进行MD5加密
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.falure(1002,"日期格式错误",null);
        }
        List<User> users = userMapper.queryUserByPhoneNumber(userRegister.getPhoneNumber());  //查询手机号是否已被注册
        if (users.size() != 0){
            log.info(userRegister.getRealName()+"注册失败,手机号已被注册");
            return Result.falure(1008,"手机号已被注册",null);
        }
        if (userMapper.addUser(user) == 0)
        {
            log.info(userRegister.getRealName()+"注册失败,数据库插入错误");
            return Result.falure(1001,"未知错误,请联系管理员",null);
        }
        log.info(userRegister.getPhoneNumber()+"注册成功");
        return Result.success("注册成功",JwtTokenUtil.getToken(String.valueOf(user.getUserId())));
    }

    @Override
    public Result login(LoginBody loginBody, String checkCode) {
        HashMap<String, String> map = new HashMap<>();
        String phone = loginBody.getPhoneNumber();
        String pwd = MD5Util.desEncrypt(loginBody.getPassWord());//解密
        String code = loginBody.getCode();
        if (!code.equals(checkCode)){
            log.info(loginBody.getPhoneNumber()+"验证码错误,登录失败");
            return Result.falure(2002,"验证码错误",null);
        }

        List<User> users = userMapper.queryUserByPhoneNumber(phone);

        if (users.size()==0){
            log.info(phone+"用户不存在");
            return Result.falure(2009,"用户不存在",null);
        }

        User user = users.get(0);

        if (user.getPhoneNumber().equals(phone) && MD5Util.reconvertMD5(user.getPassWord()).equals(pwd)){
            log.info(user.getRealName()+"登录成功");
            user.setPhoneNumber(MD5Util.convertMD5(user.getPhoneNumber()));
            user.setPassWord(MD5Util.convertMD5(user.getPassWord()));

            return Result.success("登录成功",JwtTokenUtil.getToken(String.valueOf(user.getUserId())));
        }

        log.info("用户名:"+ loginBody.getPhoneNumber()+"或密码:"+ loginBody.getPassWord()+"错误");
        return Result.falure(1009,"用户名或密码错误",null);
    }

    @Override
    public Result queryPhoneNumber() {
        return null;
    }

    @Override
    public Result updateUser() {
        return null;
    }



}
